package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.SnackbarManager;
import com.nispok.snackbar.enums.SnackbarType;
import com.nispok.snackbar.listeners.ActionClickListener;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.BaseStatistic;
import com.tianxin.workbao.workBao.bean.CarFinanceStatistic;
import com.tianxin.workbao.workBao.bean.StorageStatistic;
import com.tianxin.workbao.workBao.bean.WaitStatistic;
import com.tianxin.workbao.workBao.dataProvide.BaseStatisticProvide;
import com.tianxin.workbao.workBao.dataProvide.CarFinanceStatisticProvide;
import com.tianxin.workbao.workBao.dataProvide.StorageStatisticProvide;
import com.tianxin.workbao.workBao.dataProvide.WaitStatisticProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import lecho.lib.hellocharts.listener.PieChartOnValueSelectListener;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SelectedValue;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.view.PieChartView;


public class StatisticActivity extends BaseActivity implements UpdateListViewListener, View.OnClickListener {
    private FloatingActionButton topFab;
    private TextView statisticTimeTv;
    private TextView infoTv;
    private ImageView storageArrow;
    private ExpandView storageExpandView;
    private PieChartView storagePieChartView;
    private StorageStatisticProvide storageStatisticProvide;
    private StorageStatistic mStorageStatistic;

    private ImageView waitArrow;
    private ExpandView waitExpandView;
    private PieChartView waitPieChartView;
    private WaitStatisticProvide waitStatisticProvide;
    private WaitStatistic mWaitStatistic;
    private CircleImageView waitNextSelectValueCIV;
    private int waitLastPosition = -1;
    private int waitPosition;
    private List<SliceValue> waitValues;

    private ImageView carFinanceArrow;
    private ExpandView carFinanceExpandView;
    private PieChartView carFinancePieChartView;
    private CarFinanceStatisticProvide carFinanceStatisticProvide;
    private CarFinanceStatistic mCarFinanceStatistic;


    private ImageView baseArrow;
    private ExpandView baseExpandView;
    private PieChartView basePieChartView;
    private BaseStatisticProvide baseStatisticProvide;
    private BaseStatistic mBaseStatistic;
    private CircleImageView baseNextSelectValueCIV;
    private int baseLastPosition = -1;
    private int basePosition;
    private List<SliceValue> baseValues;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistic);
        super.initStatusBar(false);

        initView();
        initData();
        initEvent();
        bindView();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("统计");
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(AppCompatResources.getDrawable(this, R.drawable.ic_action_back_24dp));

        statisticTimeTv = (TextView) findViewById(R.id.statistic_time);
        infoTv = (TextView) findViewById(R.id.info);
        storageArrow = (ImageView) findViewById(R.id.arrow_storage);
        storageExpandView = (ExpandView) findViewById(R.id.expandView_storage);
        storageExpandView.setExpand(false);
        storagePieChartView = (PieChartView) findViewById(R.id.storage_pieChartView);

        topFab = (FloatingActionButton) findViewById(R.id.top_fab);
        // wait pie chart view
        waitArrow = (ImageView) findViewById(R.id.arrow_wait);
        waitExpandView = (ExpandView) findViewById(R.id.expandView_wait);
        waitExpandView.setExpand(true);
        waitPieChartView = (PieChartView) findViewById(R.id.pieChartView_wait);
        waitNextSelectValueCIV = (CircleImageView) findViewById(R.id.wait_next_Select_value);
        // car finance pie chart view
        carFinanceArrow = (ImageView) findViewById(R.id.arrow_car_finance);
        carFinanceExpandView = (ExpandView) findViewById(R.id.expandView_car_finance);
        carFinancePieChartView = (PieChartView) findViewById(R.id.car_finance_pieChartView);
        // base data pie chart view
        baseArrow = (ImageView) findViewById(R.id.arrow_base);
        baseExpandView = (ExpandView) findViewById(R.id.expandView_base);
        basePieChartView = (PieChartView) findViewById(R.id.pieChartView_base);
        baseNextSelectValueCIV = (CircleImageView) findViewById(R.id.next_Select_value);
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }


    @Override
    public void initData() {
        getStorageStatisticData();
        getWaitStatisticData();
        getCarFinanceStatisticData();
        getBaseStatisticData();
    }

    private void getStorageStatisticData() {
        getStorageStatisticProvide().getdatas();
    }

    private void getWaitStatisticData() {
        getWaitStatisticProvide().getdatas();
    }

    private void getCarFinanceStatisticData() {
        getCarFinanceStatisticProvide().getdatas();
    }

    private void getBaseStatisticData() {
        getBaseStatisticProvide().getdatas();
    }

    @Override
    public void initEvent() {
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StatisticActivity.this.finish();
            }
        });
        storageArrow.setOnClickListener(this);
        waitArrow.setOnClickListener(this);
        carFinanceArrow.setOnClickListener(this);
        baseArrow.setOnClickListener(this);
        topFab.setOnClickListener(this);
        baseNextSelectValueCIV.setOnClickListener(this);
        waitNextSelectValueCIV.setOnClickListener(this);
    }

    @Override
    public void bindView() {
        infoTv.setText("");
        statisticTimeTv.setText("");
    }

    @Override
    public void onBackPressed() {
        StatisticActivity.this.finish();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.arrow_storage:
                handleExpendView(storageExpandView, storageArrow);
                break;
            case R.id.arrow_wait:
                handleExpendView(waitExpandView, waitArrow);
                if (waitExpandView.isExpand()) {
                    waitNextSelectValueCIV.setVisibility(View.VISIBLE);
                } else {
                    waitNextSelectValueCIV.setVisibility(View.GONE);
                }
                break;
            case R.id.arrow_car_finance:
                handleExpendView(carFinanceExpandView, carFinanceArrow);
                break;
            case R.id.arrow_base:
                handleExpendView(baseExpandView, baseArrow);
                if (baseExpandView.isExpand()) {
                    baseNextSelectValueCIV.setVisibility(View.VISIBLE);
                } else {
                    baseNextSelectValueCIV.setVisibility(View.GONE);
                }
                break;
            case R.id.top_fab:
                handleExpendView(storageExpandView, storageArrow);
                handleExpendView(carFinanceExpandView, carFinanceArrow);
                handleExpendView(baseExpandView, baseArrow);
                getWaitStatisticData();
                break;
            case R.id.next_Select_value:
                if (baseLastPosition != -1) {
                    basePosition = baseLastPosition;
                }
                basePosition = (basePosition - 1 + baseValues.size()) % baseValues.size();
                SelectedValue selectedValue =
                        new SelectedValue(basePosition, 0, SelectedValue.SelectedValueType.NONE);
                basePieChartView.selectValue(selectedValue);
                break;
            case R.id.wait_next_Select_value:
                if (waitLastPosition != -1) {
                    waitPosition = waitLastPosition;
                }
                waitPosition = (waitPosition - 1 + waitValues.size()) % waitValues.size();
                SelectedValue waitSelectedValue =
                        new SelectedValue(waitPosition, 0, SelectedValue.SelectedValueType.NONE);
                waitPieChartView.selectValue(waitSelectedValue);
                break;
        }
    }

    @Override
    public void showMessage(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showProgress() {
        getSweetAlertDialog(this).showDialog("", 2000);
    }

    @Override
    public void hideProgress() {
        getSweetAlertDialog(this).dismiss();
    }

    @Override
    public void onRefreshData(Object result) {
        if (result instanceof StorageStatistic) {
            mStorageStatistic = (StorageStatistic) result;
            createStoragePieChartView();
        } else if (result instanceof WaitStatistic) {
            mWaitStatistic = (WaitStatistic) result;
            createWaitPieChartView();
        } else if (result instanceof CarFinanceStatistic) {
            mCarFinanceStatistic = (CarFinanceStatistic) result;
            createCarFinancePieChartView();
        } else if (result instanceof BaseStatistic) {
            mBaseStatistic = (BaseStatistic) result;
            createBasePieChartView();
        }
    }

    private void createStoragePieChartView() {
        if (mStorageStatistic == null) {
            showMessage("获取油库统计信息失败");
            return;
        }
        List<SliceValue> values = new ArrayList<>();
        SliceValue value1 = new SliceValue();
        value1.setValue(mStorageStatistic.getTotalOut());
        value1.setLabel("总加油 " + mStorageStatistic.getTotalOut() + " 升");
        value1.setColor(getResources().getColor(R.color.recycler_color3));
        values.add(value1);

        SliceValue value2 = new SliceValue();
        value2.setValue(mStorageStatistic.getTotalOut());
        value2.setLabel("总入库 " + mStorageStatistic.getTotalIn() + " 升");
        value2.setColor(getResources().getColor(R.color.recycler_color4));
        values.add(value2);

        SliceValue value3 = new SliceValue();
        value3.setValue(mStorageStatistic.getTotalOut());
        value3.setLabel("库存 " + mStorageStatistic.getCurrStorage() + " 升");
        value3.setColor(getResources().getColor(R.color.recycler_color1));
        values.add(value3);

        PieChartData data = new PieChartData();
        data.setValues(values);
        data.setSlicesSpacing(1);
        data.setHasCenterCircle(true);
        data.setCenterText2FontSize(12);
        data.setCenterText1FontSize(16);
        data.setCenterCircleColor(getResources().getColor(R.color.white));
        data.setCenterText2Color(getResources().getColor(R.color.book_title_mask));
        data.setCenterText2("当前油价");
        data.setCenterText1(mStorageStatistic.getCurrPrice() + "");
        data.setCenterText1Color(getResources().getColor(R.color.primary_text));
        storagePieChartView.setPieChartData(data);
        storagePieChartView.setOnValueTouchListener(getValueSelectListener());
        storagePieChartView.setCircleFillRatio(1);
        storagePieChartView.setChartRotation(45, false);
        storagePieChartView.setChartRotationEnabled(false);
        infoTv.setText("今天：加油 " + mStorageStatistic.getTodayOutTimes() + " 次，共 " + mStorageStatistic.getTodayOut() + " 升");
        statisticTimeTv.setText(TimeUtils.parseDateTime(mStorageStatistic.getTime()));
    }

    private void createWaitPieChartView() {
        if (mWaitStatistic == null) {
            showMessage("获取待办事项统计信息失败");
            return;
        }
        waitValues = new ArrayList<>();
        SliceValue value1 = new SliceValue();
        value1.setValue(mWaitStatistic.getInBillOauth());
        value1.setLabel("入库数据未审核 " + mWaitStatistic.getInBillOauth());
        value1.setColor(getResources().getColor(R.color.drawer_text));
        waitValues.add(value1);

        SliceValue value2 = new SliceValue();
        value2.setValue(mWaitStatistic.getOutBillOauth());
        value2.setLabel("出库数据未审核 " + mWaitStatistic.getOutBillOauth());
        value2.setColor(getResources().getColor(R.color.friend_header));
        waitValues.add(value2);

        SliceValue value3 = new SliceValue();
        value3.setValue(mWaitStatistic.getCarOut());
        value3.setLabel("车辆未出场 " + mWaitStatistic.getCarOut());
        value3.setColor(getResources().getColor(R.color.lunch_header));
        waitValues.add(value3);

        SliceValue value4 = new SliceValue();
        value4.setValue(mWaitStatistic.getBack());
        value4.setLabel("未录入回单 " + mWaitStatistic.getBack());
        value4.setColor(getResources().getColor(R.color.midnight_snack_header));
        waitValues.add(value4);

        SliceValue value5 = new SliceValue();
        value5.setValue(mWaitStatistic.getCpuCards());
        value5.setLabel("车辆未发卡 " + mWaitStatistic.getCpuCards());
        value5.setColor(getResources().getColor(R.color.bg_item_dragging_active_state));
        waitValues.add(value5);

        SliceValue value6 = new SliceValue();
        value6.setValue(mWaitStatistic.getLoaderCar());
        value6.setLabel("未装车 " + mWaitStatistic.getLoaderCar());
        value6.setColor(getResources().getColor(R.color.bg_item_swiping_active_state));
        waitValues.add(value6);

        SliceValue value7 = new SliceValue();
        value7.setValue(mWaitStatistic.getSampling());
        value7.setLabel("未采样 " + mWaitStatistic.getSampling());
        value7.setColor(getResources().getColor(R.color.normal_button_press));
        waitValues.add(value7);

        SliceValue value8 = new SliceValue();
        value8.setValue(mWaitStatistic.getTest());
        value8.setLabel("未化验 " + mWaitStatistic.getTest());
        value8.setColor(getResources().getColor(R.color.bg_swipe_group_item_pinned));
        waitValues.add(value8);

        SliceValue value9 = new SliceValue();
        value9.setValue(mWaitStatistic.getOilBillOauth());
        value9.setLabel("加油单未审核 " + mWaitStatistic.getOilBillOauth());
        value9.setColor(getResources().getColor(R.color.logout_button_press));
        waitValues.add(value9);

        SliceValue value10 = new SliceValue();
        value10.setValue(mWaitStatistic.getTransMoney());
        value10.setLabel("未结算运费 " + mWaitStatistic.getTransMoney());
        value10.setColor(getResources().getColor(R.color.recycler_color1));
        waitValues.add(value10);

        SliceValue value11 = new SliceValue();
        value11.setValue(mWaitStatistic.getCoalMoney());
        value11.setLabel("未结算煤款 " + mWaitStatistic.getCoalMoney());
        value11.setColor(getResources().getColor(R.color.recycler_color4));
        waitValues.add(value11);

        SliceValue value12 = new SliceValue();
        value12.setValue(mWaitStatistic.getCoalSettlementOauth());
        value12.setLabel("煤款结算未审核 " + mWaitStatistic.getCoalSettlementOauth());
        value12.setColor(getResources().getColor(R.color.recycler_color2));
        waitValues.add(value12);

        SliceValue value13 = new SliceValue();
        value13.setValue(mWaitStatistic.getCoalPay());
        value13.setLabel("未收付款 " + mWaitStatistic.getCoalPay());
        value13.setColor(getResources().getColor(R.color.recycler_color3));
        waitValues.add(value13);

        SliceValue value14 = new SliceValue();
        value14.setValue(mWaitStatistic.getCoalPayOauth());
        value14.setLabel("收付款审核 " + mWaitStatistic.getCoalPayOauth());
        value14.setColor(getResources().getColor(R.color.alertDialog_Progress_Color));
        waitValues.add(value14);

        PieChartData data = new PieChartData();
        data.setValues(waitValues);
        data.setSlicesSpacing(1);
        data.setHasCenterCircle(true);
        data.setCenterText2FontSize(11);
        data.setCenterText1FontSize(16);
        data.setCenterCircleColor(getResources().getColor(R.color.white));
        data.setCenterText2Color(getResources().getColor(R.color.book_title_mask));
        data.setCenterText2(TimeUtils.parseDate(mWaitStatistic.getTime()));
        data.setCenterText1("待办");
        data.setCenterText1Color(getResources().getColor(R.color.primary_text));
        waitPieChartView.setPieChartData(data);
        waitPieChartView.setOnValueTouchListener(getValueSelectListener());
        waitPieChartView.setCircleFillRatio(1);
        waitPieChartView.setChartRotation(45, false);
        waitPieChartView.setChartRotationEnabled(false);
    }

    private void createCarFinancePieChartView() {
        if (mWaitStatistic == null) {
            showMessage("获取车辆资金统计信息失败");
            return;
        }
        List<SliceValue> values = new ArrayList<>();
        SliceValue value1 = new SliceValue();
        value1.setValue(mCarFinanceStatistic.getInMoney());
        value1.setLabel("总入账 " + mCarFinanceStatistic.getInMoney());
        value1.setColor(getResources().getColor(R.color.recycler_color1));
        values.add(value1);

        SliceValue value2 = new SliceValue();
        value2.setValue(mCarFinanceStatistic.getOutMoney());
        value2.setLabel("总出账 " + mCarFinanceStatistic.getOutMoney());
        value2.setColor(getResources().getColor(R.color.recycler_color3));
        values.add(value2);

        SliceValue value3 = new SliceValue();
        value3.setValue(mCarFinanceStatistic.getMoney());
        value3.setLabel("总余额 " + mCarFinanceStatistic.getMoney());
        value3.setColor(getResources().getColor(R.color.recycler_color4));
        values.add(value3);


        PieChartData data = new PieChartData();
        data.setValues(values);
        data.setSlicesSpacing(1);
        data.setHasCenterCircle(true);
        data.setCenterText2FontSize(11);
        data.setCenterText1FontSize(16);
        data.setCenterCircleColor(getResources().getColor(R.color.white));
        data.setCenterText2Color(getResources().getColor(R.color.book_title_mask));
        data.setCenterText2(TimeUtils.parseDate(mCarFinanceStatistic.getTime()));
        data.setCenterText1("车辆资金");
        data.setCenterText1Color(getResources().getColor(R.color.primary_text));
        carFinancePieChartView.setPieChartData(data);
        carFinancePieChartView.setOnValueTouchListener(getValueSelectListener());
        carFinancePieChartView.setCircleFillRatio(1);
        carFinancePieChartView.setChartRotation(45, false);
        carFinancePieChartView.setChartRotationEnabled(false);
    }

    private void createBasePieChartView() {
        if (mWaitStatistic == null) {
            showMessage("获取基础数据统计信息失败");
            return;
        }
        baseValues = new ArrayList<>();
        SliceValue value1 = new SliceValue();
        value1.setValue(mBaseStatistic.getCars());
        value1.setLabel("拥有车辆 " + mBaseStatistic.getCars());
        value1.setColor(getResources().getColor(R.color.recycler_color1));
        baseValues.add(value1);

        SliceValue value2 = new SliceValue();
        value2.setValue(mBaseStatistic.getStaffs());
        value2.setLabel("使用系统的员工 " + mBaseStatistic.getStaffs());
        value2.setColor(getResources().getColor(R.color.recycler_color3));
        baseValues.add(value2);

        SliceValue value3 = new SliceValue();
        value3.setValue(mBaseStatistic.getCustomers());
        value3.setLabel("公司名称数 " + mBaseStatistic.getCustomers());
        value3.setColor(getResources().getColor(R.color.recycler_color4));
        baseValues.add(value3);

        SliceValue value4 = new SliceValue();
        value4.setValue(mBaseStatistic.getCoals());
        value4.setLabel("煤种数 " + mBaseStatistic.getCoals());
        value4.setColor(getResources().getColor(R.color.drawer_text));
        baseValues.add(value4);

        SliceValue value5 = new SliceValue();
        value5.setValue(mBaseStatistic.getTestItems());
        value5.setLabel("化验指标项 " + mBaseStatistic.getTestItems());
        value5.setColor(getResources().getColor(R.color.baby_header));
        baseValues.add(value5);

        SliceValue value6 = new SliceValue();
        value6.setValue(mBaseStatistic.getLoaders());
        value6.setLabel("装载机数 " + mBaseStatistic.getLoaders());
        value6.setColor(getResources().getColor(R.color.bg_group_item_dragging_active_state));
        baseValues.add(value6);

        SliceValue value7 = new SliceValue();
        value7.setValue(mBaseStatistic.getLoaders());
        value7.setLabel("装载机数 " + mBaseStatistic.getLoaders());
        value7.setColor(getResources().getColor(R.color.bg_group_item_expanded_state));
        baseValues.add(value7);

        PieChartData data = new PieChartData();
        data.setValues(baseValues);
        data.setSlicesSpacing(1);
        data.setHasCenterCircle(true);
        data.setCenterText2FontSize(11);
        data.setCenterText1FontSize(16);
        data.setCenterCircleColor(getResources().getColor(R.color.white));
        data.setCenterText2Color(getResources().getColor(R.color.book_title_mask));
        data.setCenterText2(TimeUtils.parseDate(mBaseStatistic.getTime()));
        data.setCenterText1("基础数据");
        data.setCenterText1Color(getResources().getColor(R.color.primary_text));
        basePieChartView.setPieChartData(data);
        basePieChartView.setOnValueTouchListener(getValueSelectListener());
        basePieChartView.setCircleFillRatio(1);
        basePieChartView.setChartRotation(45, false);
        basePieChartView.setChartRotationEnabled(false);
    }

    @Override
    public void onLoadMoreData(Object result) {

    }

    private StorageStatisticProvide getStorageStatisticProvide() {
        if (storageStatisticProvide == null) {
            storageStatisticProvide = new StorageStatisticProvide(this, this);
        }
        return storageStatisticProvide;
    }

    private WaitStatisticProvide getWaitStatisticProvide() {
        if (waitStatisticProvide == null) {
            waitStatisticProvide = new WaitStatisticProvide(this, this);
        }
        return waitStatisticProvide;
    }

    private CarFinanceStatisticProvide getCarFinanceStatisticProvide() {
        if (carFinanceStatisticProvide == null) {
            carFinanceStatisticProvide = new CarFinanceStatisticProvide(this, this);
        }
        return carFinanceStatisticProvide;
    }

    private BaseStatisticProvide getBaseStatisticProvide() {
        if (baseStatisticProvide == null) {
            baseStatisticProvide = new BaseStatisticProvide(this, this);
        }
        return baseStatisticProvide;
    }

    public PieChartOnValueSelectListener getValueSelectListener() {
        PieChartOnValueSelectListener listener = new PieChartOnValueSelectListener() {
            @Override
            public void onValueSelected(int arcIndex, final SliceValue value) {
                com.nispok.snackbar.Snackbar snackbar =
                        com.nispok.snackbar.Snackbar
                                .with(StatisticActivity.this)
                                .type(SnackbarType.MULTI_LINE)
                                .duration(com.nispok.snackbar.Snackbar.SnackbarDuration.LENGTH_SHORT)
                                .position(com.nispok.snackbar.Snackbar.SnackbarPosition.BOTTOM)
                                .margin(15, 15)
                                .backgroundDrawable(R.color.colorPrimary)
                                .text(String.valueOf(value.getLabel()))
                                .textColor(Color.WHITE)
                                .actionLabel("查看")
                                .actionColor(Color.WHITE)
                                .actionListener(new ActionClickListener() {
                                    @Override
                                    public void onActionClicked(Snackbar snackbar) {
                                        Toast.makeText(StatisticActivity.this, String.valueOf(value.getLabel()), Toast.LENGTH_SHORT).show();
                                    }
                                });

                SnackbarManager.show(snackbar);
            }

            @Override
            public void onValueDeselected() {

            }
        };
        return listener;
    }

}
